package com.aratek.salary.common;

import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Iterator;

/**
 * @author Yvan Jiang
 * @version 0.0.1
 * @Title PoiUtils
 * @note 此处进行文件说明
 * @note Copyright 2016 by Aratek . All rights reserved
 * @time 2017/11/16
 **/
public class PoiUtils {

    private static final Logger LOG = LoggerFactory.getLogger(PoiUtils.class);
    /**
     * 行复制功能
     *  @param fromRow
     * @param formulaEvaluator
     * @param toRow
     */
    public static void copyRow(Workbook wb, Row fromRow, FormulaEvaluator formulaEvaluator, Row toRow, boolean copyValueFlag) {
        toRow.setHeight(fromRow.getHeight());
        for(Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext(); ) {
            Cell tmpCell = (Cell) cellIt.next();
            if(tmpCell.getColumnIndex() == 40){
                //不复制邮箱地址
                break ;
            }
            Cell newCell = toRow.createCell(tmpCell.getColumnIndex());
            copyCell(wb, tmpCell, newCell, formulaEvaluator, copyValueFlag);
        }
    }
    
    /**
     * 复制单元格
     *
     * @param srcCell
     * @param distCell
     * @param copyValueFlag true则连同cell的内容一起复制
     */
    public static void copyCell(Workbook wb, Cell srcCell, Cell distCell,FormulaEvaluator formulaEvaluator, boolean copyValueFlag) {
        CellStyle newStyle = wb.createCellStyle();
        CellStyle srcStyle = srcCell.getCellStyle();
        newStyle.cloneStyleFrom(srcStyle);
        newStyle.setFont(wb.getFontAt((short) Math.min(wb.getNumberOfFonts()-1,srcStyle.getFontIndex())));
        //样式
        distCell.setCellStyle(newStyle);
        //评论
        if(srcCell.getCellComment() != null) {
            distCell.setCellComment(srcCell.getCellComment());
        }
        // 不同数据类型处理
        CellType srcCellType = srcCell.getCellTypeEnum();
        distCell.setCellType(srcCellType);
        if(copyValueFlag) {
            if(srcCellType == CellType.NUMERIC) {
                if(DateUtil.isCellDateFormatted(srcCell)) {
                    distCell.setCellValue(srcCell.getDateCellValue());
                } else {
                    distCell.setCellValue(srcCell.getNumericCellValue());
                }
            } else if(srcCellType == CellType.STRING) {
                distCell.setCellValue(srcCell.getRichStringCellValue());
            } else if(srcCellType == CellType.BLANK) {
            
            } else if(srcCellType == CellType.BOOLEAN) {
                distCell.setCellValue(srcCell.getBooleanCellValue());
            } else if(srcCellType == CellType.ERROR) {
                distCell.setCellErrorValue(srcCell.getErrorCellValue());
            } else if(srcCellType == CellType.FORMULA) {
                double d = formulaEvaluator.evaluate(srcCell).getNumberValue();
                distCell.setCellType(CellType.NUMERIC);
                distCell.setCellValue(d);
            } else {
                LOG.warn("Get other info {} {}",srcCell.getCellTypeEnum().name(),srcCell.getRichStringCellValue());
            }
        }
    }

    public static String format(double value) {
        DecimalFormat df = new DecimalFormat("0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(value);
    }
}
